package com.chenxi.vhr.controller.system.basic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenxi.vhr.bean.VhrPosition;
import com.chenxi.vhr.bean.common.NasDeleteListForm;
import com.chenxi.vhr.bean.common.Result;
import com.chenxi.vhr.service.VhrPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author : zhangchenxi
 * @create : 2022-02-04
 **/
@RestController
@RequestMapping("/system/basic/position")
public class PositionController {

    @Autowired
    VhrPositionService positionService;

    @GetMapping("/")
    public Result list () {
        List<VhrPosition> list = positionService.list();
        return Result.ok("职位查询成功", list);
    }

    @PostMapping("/")
    public Result add (@RequestBody VhrPosition position) {
        if (position == null) {
            return Result.error("提交数据异常！");
        }
        if (StringUtils.isEmpty(position.getName())) {
            return Result.error("职位名称不能为空！");
        }
        QueryWrapper<VhrPosition> queryWrapper = new QueryWrapper<VhrPosition>();
        queryWrapper.eq("name", position.getName());
        VhrPosition po = positionService.getOne(queryWrapper);
        if (po != null) {
            return Result.error("职位名称不可重复！");
        }
        position.setEnabled(true);
        position.setCreateDate(new Date());
        boolean flag = positionService.saveOrUpdate(position);
        if (flag) {
            return Result.ok("职位添加成功");
        } else {
            return Result.error("职位添加失败");
        }
    }

    @PutMapping("/")
    public Result update(@RequestBody VhrPosition position) throws Exception {
        if (position == null) {
            return Result.error("提交数据异常！");
        }
        if (StringUtils.isEmpty(position.getName())) {
            return Result.error("职位名称不能为空！");
        }
        if (StringUtils.isEmpty(position.getId())) {
            return Result.error("职位Id不能为空！");
        }
        VhrPosition po = positionService.getById(position.getId());
        if (po == null) {
            return Result.error("修改的数据不存在");
        }
        boolean flag = positionService.saveOrUpdate(position);
        if (flag) {
            return Result.ok("职位更新成功");
        } else {
            return Result.error("职位更新失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception {
        if (true) {
            // 模拟外键删除失败，本系统没有外键，但是个别系统有外键的情况
            // throw new MySQLIntegrityConstraintViolationException("删除失败");
        }
        boolean flag = positionService.removeById(id);
        return flag ? Result.ok("职位删除成功") : Result.error("职位删除失败");
    }

    @DeleteMapping("/batch")
    public Result batchDelete(@RequestBody NasDeleteListForm form) {
        return positionService.deleteBatchByIdList(form.getIdList());
    }


}
